//
//  HELPDFfileCacheTool.m
//  sanda
//
//  Created by wsj2015 on 16/7/20.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELPDFfileCacheTool.h"
#import "RLMCourseManager.h"
@implementation HELPDFfileCacheTool


+(NSString *)isChchedWithPdfFilename:(NSString *)courseName isMP4:(BOOL)isMP4{

    if (!courseName) {
        return nil;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    
    
    NSString *filePath = [self getDownloadPathWithPdfName:courseName isMp4:isMP4];
    if ([fm fileExistsAtPath:filePath]) {
       
        return  filePath;
    }
    return nil;
}

+ (NSString *)getDownloadPathWithPdfName:(NSString *)pdfName isMp4:(BOOL)isMp4{
    
   
    
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString *downloadURL;
    if (isMp4) {
       downloadURL  =[cachesDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.MP4",pdfName]];
    }else{
       downloadURL =[cachesDir stringByAppendingPathComponent:pdfName];
    }
  
    return downloadURL;
}



+ (NSNumber *)getLastPageFromKey:(NSString *)pdfKey{

 return  [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%@",pdfKey,BT_USERID]];
}
+ (void)saveLastPage:(NSInteger)page withPdfKey:(NSString *)pdfKey
{
    [[NSUserDefaults standardUserDefaults] setObject:@(page) forKey:[NSString stringWithFormat:@"%@%@",pdfKey,BT_USERID]];
    [[NSUserDefaults standardUserDefaults] synchronize];
   
}


+ (void)removePdfFileWithPdfNames:(NSArray<BTCourseModel *>*)courses{
  
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [courses enumerateObjectsUsingBlock:^(BTCourseModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            BOOL isMP4 = NO;
            if ([obj.courseType integerValue]) {
                isMP4 = YES;
            }
           NSString *path = [self getDownloadPathWithPdfName:obj.courseName isMp4:isMP4];
            if (path) {
                NSFileManager *fm = [NSFileManager defaultManager];
                [fm removeItemAtPath:path error:nil];
            }
        }];
    });
   
}

+ (void)sizeForAllCourseWithCallback:(void(^)(CGFloat size))callBack{
   
 
    NSFileManager *fm = [NSFileManager defaultManager];
    __block long long size = 0;
    [[RLMCourseManager manager] getAllObjectsWithHandel:^(NSArray<BTCourseModel *> *arr) {
        if (arr.count == 0) {
            callBack(0);
        }
          [arr enumerateObjectsUsingBlock:^(BTCourseModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
              BOOL isMP4 = NO;
              if ([obj.courseType integerValue] ==1) {
                  isMP4 = YES;
              }
              
              NSString *path = [self getDownloadPathWithPdfName:obj.courseName isMp4:isMP4];
              if ([fm fileExistsAtPath:path]) {
                 size +=[fm attributesOfItemAtPath:path error:nil].fileSize;
              }
              if (idx == arr.count-1) {
                  
                  callBack(size /(1024*1024));
              }
          }];
    }];
    
}

+ (void)removeAllCourses:(void(^)())callBack{
    
    NSFileManager *fm = [NSFileManager defaultManager];
    [[RLMCourseManager manager] getAllObjectsWithHandel:^(NSArray<BTCourseModel *> *arr) {
        [arr enumerateObjectsUsingBlock:^(BTCourseModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BOOL isMP4 = NO;
            if ([obj.courseType integerValue] ==1) {
                isMP4 = YES;
            }
            NSString *path = [self getDownloadPathWithPdfName:obj.courseName isMp4:isMP4];
            if ([fm fileExistsAtPath:path]) {
                [fm removeItemAtPath:path error:nil];
            }
            
            if (idx == arr.count-1) {
                
                callBack();
            }
        }];
    }];


}




@end
